package com.zjf.controller;

import com.zjf.common.Result;
import com.zjf.entity.Cart;
import com.zjf.service.ICartService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/cart")
@CrossOrigin
public class CartController {
    @Resource
    private ICartService cartService;


    @PostMapping("/add")
    public Result<Cart> add(@RequestBody Cart cart){
        int id=cartService.saveCart(cart);
        if (id==0){
            return Result.fail("新增失败");
        }
        return Result.success(cart,"新增成功");
    }
    @PutMapping("/update")
    public Result<?> update(@RequestBody Cart cart){
        int id=cartService.updateCart(cart);
        if (id==0){
            return Result.fail("更新失败");
        }
        return Result.success(cart,"修改成功");
    }
    
    @DeleteMapping("/delete")
    public Result<?> delete(@RequestBody Cart cart){
        int id=cartService.removeCart(cart);
        if (id==0){
            Result.fail("删除失败");
        }
        return Result.success(cart,"删除成功") ;
    }
    
    @GetMapping("/list")
    public Result<List<Cart>> list(@RequestParam("uId") String uId,
                                   @RequestParam(value = "bId", required = false) Integer bId){
        List<Cart> listCart=cartService.listCart(uId,bId);
        return Result.success(listCart,"查询成功") ;
    }


  


}
